package com.zpkj.portal.utils;

import org.apache.commons.lang.StringUtils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

public class PinyinUtils {

	/**
     * 将字符串中的中文转化为拼音,其他字符不变
     * @param inputString
     * @return
     */
    public static String getPinYin(String inputString){
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);//小写LOWERCASE;大写UPPERCASE
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//不知道这个是什么意思 写完查api看看
        format.setVCharType(HanyuPinyinVCharType.WITH_V);//
        char[] input = inputString.trim().toCharArray();
        String output="";
        try {
            for(int i = 0; i < input.length; i++){
                if(java.lang.Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")){
                        String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i],format);
                        output+=temp[0];
                }else{
                    output+=input[i];
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return output;
    }
    
    /**
     * 获取首字母
     */
    public static String getFirstLetter(String str){
    	try{
	    	if(StringUtils.isEmpty(str)){
	    		return "#";
	    	}
	    	str = PinyinUtils.getPinYin(str);
	    	String newStr = str.trim().substring(0,1);
	    	return teshuzifu(newStr);
    	}catch(Exception e){
    		return "#";
    	}
    }
    
    /**
     * 特殊字符
     */
    public static String teshuzifu(String str){
    	String regEx="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	if(!regEx.contains(str)){
    		return "#";
    	}
    	return str;
    } 
    
    /*public static void main(String[] args) {
		String str = "巭孬";
		System.out.println(PinyinUtils.getFirstLetter(str));
	}*/

}
